package com.cater.task.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.sys.service.UserService;
import com.cater.task.entity.TaskManager;
import com.cater.task.entity.TaskSubManager;
import com.cater.task.service.TaskManagerServiceImpl;
import com.cater.task.service.TaskSubManagerServiceImpl;

/**
 * 描述：任务子表控制层
 * 
 * @author yong
 * @date 2019/04/14
 */
@Controller
@RequestMapping("/taskSubManager")
public class TaskSubManagerController {

	@Autowired
	private TaskSubManagerServiceImpl service;
	@Autowired
	private TaskManagerServiceImpl taskManagerService;
	@Autowired
	private UserService userService;

	/**
	 * @任务子表管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list(CommonQuery query, Model model) {
		String taskId = query.getTaskId();
		if (StringUtils.isNotEmpty(taskId)) {
			TaskManager task = taskManagerService.getById(taskId);
			if (task != null) {
				model.addAttribute("task", task);
			}
		}
		return "task/taskSubManager/list";
	}

	/**
	 * @任务子表管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询任务子表分页数据")
	public PageResult<TaskSubManager> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<TaskSubManager> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("taskSubManager:add")
	public String addView(CommonQuery query, Model model) {
		String taskId = query.getTaskId();
		if (StringUtils.isNotEmpty(taskId)) {
			TaskManager task = taskManagerService.getById(taskId);
			if (task != null) {
				String[] userIds = task.getParticipant().split(",");
				if (userIds != null && userIds.length > 0) {
					List<Map<String, String>> users = userService.getByArrIds(userIds);
					if (users.size() > 0) {
						model.addAttribute("users", users);
					}
				}
				model.addAttribute("task", task);
			}
		}
		return "task/taskSubManager/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("taskSubManager:add")
	@ResponseBody
	@SystemControllerLog(description = "添加任务子表")
	public JSONObject add(TaskSubManager t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("taskSubManager:edit")
	public String editView(String id, Model model, CommonQuery query) {
		TaskSubManager t = service.getById(id);
		model.addAttribute("taskSubManager", t);
		return "task/taskSubManager/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("taskSubManager:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改任务子表")
	public JSONObject update(TaskSubManager t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("taskSubManager:delete")
	@SystemControllerLog(description = "删除任务子表")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("taskSubManager:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		TaskSubManager t = service.getById(id);
		model.addAttribute("taskSubManager", t);
		return "task/taskSubManager/detail";
	}

	@RequestMapping("/statusView")
	public String statusView(String id, Model model) {
		TaskSubManager t = service.getById(id);
		model.addAttribute("taskSubManager", t);
		return "task/taskSubManager/update_status";
	}

	@ResponseBody
	@RequestMapping("/updateStatus")
	public JSONObject updateStatus(String id, String status) {
		JSONObject json = new JSONObject();
		try {
			TaskSubManager t = service.getById(id);
			if (t != null) {
				t.setStatus(status);
				String taskId = t.getTaskId();
				if (StringUtils.isNotEmpty(taskId)) {
					TaskManager taskManager = taskManagerService.getById(taskId);
					if (taskManager != null) {
						/**
						 * 进行中
						 */
						taskManager.setStatus(1);
						taskManagerService.update(taskManager);
					}
				}
				service.update(t);
				json.put("code", 200);
				json.put("message", "状态修改成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 400);
			json.put("message", "状态修改失败");
		}
		return json;

	}
}